/**
 * @purpose
 * @author daizs
 * @created Mar 3, 2010
 */
package platform.common.dao.impl;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.GenericJDBCException;
import org.hibernate.exception.JDBCConnectionException;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.SQLGrammarException;

import platform.common.dao.impl.BatchOperateDaoImpl;
import platform.common.dao.impl.CriticalTechnicalDaoException;
import platform.common.dao.impl.DaoException;
import platform.common.dao.impl.SessionAndTxForBatch;
import platform.common.dao.impl.TechnicalDaoException;


public class BatchOperateDaoImpl {
	/**
	 * log4j进行日志记录。
	 */
	private Logger log = Logger.getLogger(BatchOperateDaoImpl.class);
	private static final ThreadLocal<SessionAndTxForBatch> sessionPool = new ThreadLocal<SessionAndTxForBatch>();

	private SessionFactory sessionFactory;


	public void insert(Object entity)
			throws DaoException {
		log.debug("BatchOperateDaoImpl插入临时嵌套集合树节点" + entity);
		try {
			StatelessSession session=currentSession();
			session.insert(entity);
		} catch (JDBCConnectionException e) {
			log.error("指明底层的JDBC通讯出现错误。", e);
			throw new CriticalTechnicalDaoException(e);
		} catch (SQLGrammarException e) {
			log.error("指明发送的SQL语句的语法或者格式错误。", e);
			throw new TechnicalDaoException(e);
		} catch (ConstraintViolationException e) {
			log.error("指明某种类型的约束违例错误。", e);
			throw new TechnicalDaoException(e);
		} catch (LockAcquisitionException e) {
			log.error("指明了在执行请求操作时，获取 所需的锁级别时出现的错误。", e);
			throw new TechnicalDaoException(e);
		} catch (GenericJDBCException e) {
			log.error("不属于任何其他种类的原生异常。", e);
			throw new TechnicalDaoException(e);
		} catch (Exception e) {
			log.error("其他类型的异常。", e);
			throw new DaoException(e);
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void closeCurrentSessionAndRollback() throws DaoException {
		SessionAndTxForBatch s = sessionPool.get();
		if (s != null) {
			s.getTx().rollback();
			s.getSession().close();
			s=null;
		}
		sessionPool.set(null);
	}

	public void closeCurrentSessionAndCommit() throws DaoException {
		SessionAndTxForBatch s = sessionPool.get();
		if (s != null) {
			s.getTx().commit();
			s.getSession().close();
			s=null;
		}
		sessionPool.set(null);
	}

	public StatelessSession currentSession() throws DaoException {
		SessionAndTxForBatch s = sessionPool.get();
		if (s == null) {
			log.debug("为当前线程创建StatelessSession,并开启事务");
			StatelessSession session=sessionFactory.openStatelessSession();
			Transaction tx=session.beginTransaction();
			s=new SessionAndTxForBatch();
			s.setSession(session);
			s.setTx(tx);
			sessionPool.set(s);

		}
		return s.getSession();
	}

	public void update(Object entity) throws DaoException {
		log.debug("BatchOperateDaoImpl 更新对象"+entity);
		try {
			StatelessSession session=currentSession();
			session.update(entity);
		} catch (JDBCConnectionException e) {
			log.error("指明底层的JDBC通讯出现错误。", e);
			throw new CriticalTechnicalDaoException(e);
		} catch (SQLGrammarException e) {
			log.error("指明发送的SQL语句的语法或者格式错误。", e);
			throw new TechnicalDaoException(e);
		} catch (ConstraintViolationException e) {
			log.error("指明某种类型的约束违例错误。", e);
			throw new TechnicalDaoException(e);
		} catch (LockAcquisitionException e) {
			log.error("指明了在执行请求操作时，获取 所需的锁级别时出现的错误。", e);
			throw new TechnicalDaoException(e);
		} catch (GenericJDBCException e) {
			log.error("不属于任何其他种类的原生异常。", e);
			throw new TechnicalDaoException(e);
		} catch (Exception e) {
			log.error("其他类型的异常。", e);
			throw new DaoException(e);
		}
	}



}
